<?php

require_once('includes/init.inc.php');
require_once('classes/TMSFunctions.cls.php');
require_once('classes/User.cls.php');
require_once('classes/Project.cls.php');
require_once('classes/Task.cls.php');
require_once('classes/Page.cls.php');



if(!(isset($_SESSION['username'])))
{
	header('Location:index.php');
	die();
}
$username=$_SESSION['username'];
$user = new User(TMSFunctions::getIdFromUsername($username));
$page = new Page();

if(isset($_POST['completed']))
{
	$tsk = new Task($_POST['completed']);
	
	$status=$tsk->completed();
	if($status==true)
		echo "1";
	else
		echo "0";
	die;
}

if(isset($_REQUEST['getProjectName']))
{
	echo Project::getProjectNameById($_REQUEST['getProjectName']);
	die;
}

if(isset($_REQUEST['getModules']))
{
	$pobj = new Project($_REQUEST['getModules']);

	$modules=$pobj->getModules();
	
	echo TMSFunctions::generateSelectFromDb($modules,"tms_project_modules","id","module_name","id","","--MODULES--","filters","sorty(this);","","module");

	die;

}





if(isset($_REQUEST['selectProject']))
{

	$pjt = $_REQUEST['selectProject'];
	$pid=$pjt;

	$task=$user->getTaskByProject($pjt ,'task_name',false,"");
	$page->showTable($pjt,$task);

	die;

}


if(isset($_REQUEST['updateTasks']))
{
	$sorty = $_POST['sort'];
	$desc = $_POST['desc'];
	$search = $_POST['search'];

	$module =$_POST['module'];
	$status = $_POST['status'];
	$priority = $_POST['priority'];

	$pjt = $_REQUEST['updateTasks'];

	if($desc=="false")
		$desc=false;
	else
		$desc=true;
	
	$task=$user->getTaskByProject($pjt,$sorty,$desc,$search,$module,$status,$priority);
	
	$page->showTable($pjt,$task);

	die;

}

if(isset($_REQUEST['addTaskTable']))
{

	$pjt = $_REQUEST['addTaskTable'];
	$page ->showAddTaskTable($pjt);	
	die();
}

if(isset($_REQUEST['addTask']))
{
	$tname= $_POST['tname'];
	$tdesc = $_POST['tdesc'];
	$assignee = $_POST['tassignee']; 
	$priority = $_POST['tpriority'];
	$module = $_POST['tmodule'];
	$deadline = $_POST['tdeadline'];
	$userid = TMSFunctions::getIdFromUsername($username);
	Task::addTask ($module,$tname,$userid,$assignee,$deadline,$deadline,$priority,"CLOSED",$tdesc);
	die();
}


if(isset($_POST['requestExtnTable']))
{
	$page->showReqExnTable();	
	die();
}


if(isset($_POST['topen']))
{
	$to= new Task($_POST['topen']);
	$to->open();
	echo $to->getTaskPercentage();
	die();
}


if(isset($_POST['save']))
{

$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$user->editProfile($firstname,$lastname,$email);
$page->showProfileTable($user);
die();
}


if(isset($_POST['change']))
{
$old=md5($_POST['old']);
$new=md5($_POST['newp']);
$change=$user->changePassword($old,$new);
if($change==true)
	echo 1;
else
	echo 0;
	

die();
}

if(isset($_POST['update']))
{
	$id= $_POST['update'];
	$to= new Task($id);
	echo $to->getTaskName();
		
	die();
}
if(isset($_POST['progress']))
{
	$percentage=$_POST['percentage'];
	$id=$_POST['id'];
	$t=new task($id);
	$t->setPercentage($percentage);
	if($percentage==100)
	$t->completed();
	
	die();
}


$page->showHeader($user);
?>



			
			<div id="mainmenu" class="mainmenu"><!--mainmenu-->
				
				<div id="colortab" class="ddcolortabs">
					<ul>
						<li><a href="" title="PROJECTS" rel="dropmenu1_a">
						<span>PROJECTS</span></a></li>
						<li><a class="pointer" onclick="openPopup('profilePopup')" title="PROFILE"><span>PROFILE</span></a></li>	
					</ul>
				</div>
									
									



				<!--1st drop down menu -->                                                   
				<div id="dropmenu1_a" class="dropmenudiv_a">
				
					<?php 
						$projects=$user->getProjects();
						foreach($projects as $projId)
						{ 
							echo "<a class='pointer' name='$projId' id='$projId' onClick='selectProject(this)' >".Project::getProjectNameById($projId).'</a>';

						}


						
						$pid=$projects[0];

						$pobj= new project($pid);
						

					?>

									<input type="hidden" id="sort" value="task_name"/>
									<input type="hidden" id="projectid" value="<?php echo $pid;?>"/>
									<input type="hidden" id ="desc" value="false"/>
				</div>



				<script type="text/javascript">
				//SYNTAX: tabdropdown.init("menu_id", [integer OR "auto"])
				tabdropdown.init("colortab", 3)
				</script>









			</div><!--mainmenu-->
			<div class="clear"></div>
				<div class="submenu" id="submenu">
					<div id="tasks">Project:  <?php echo $pobj->getProjectName();
						 ?> 
					</div>
					<div id="task_buttons">
						<button onclick="showAddTask()">ADD TASK</button>
						<button id="completed">COMPLETED</button>
						<button onclick="showReqExn()">REQUEST EXTENSION</button>
						<button id="updateProgress">UPDATE PROGRESS</button>
					</div>
				</div>


				<div id="dynamic">
					<div id="filter">
						
							<span>
								Search :
						
								<input type="text" maxlength="100" size="25" name="search" id="searchString" />
								<input type="submit" value="Search" onclick="search()" />
								<input type="reset" value="Reset" id="resetButton" />
				
							</span>
						
						<span style="margin-top:5px;display:block;">
						
							Filter by : 
											
							<?php
								$modules=$pobj->getModules();
								$modNames="--MODULES--";
								$modValues="--MODULES--";
								foreach($modules as $module2)
								{
									$modNames.=",";
									$modValues.=",";
													
									$modNames.=TMSFunctions::getModuleNameFromId($module2);
									$modValues.=$module2;
								}


								echo "<span id='modulesDiv'>".TMSFunctions::generateSelect($modNames,$modValues,"","filters","sorty(this);","","module")."</span>";
								
								$statusFields=Task::getStatusFields();

								echo TMSFunctions::generateSelect($statusFields,$statusFields,"","filters","sorty(this);","","status");
								$priorityFields="--PRIORITY--,HIGH,NORMAL,LOW";
								 echo TMSFunctions::generateSelect($priorityFields,$priorityFields,"","filters","sorty(this);","","priority");

							?>
						</form>
					
					</div>

					<div id="msg"  class='no2'>

					</div>

					<div id="task_details"><!--task-details-->	
					
															
						<div id='tablediv' >
								<?php
									
									$sort="task_name";
				
									$task=$user->getTaskByProject($pid,$sort,false,"");

									if(sizeof($task)==0)
									echo "<tr><div name='no' class='no'>No Records Found!!!!!</div></tr>";
									else
									{
								
									$page->showTable($pid,$task);	

									}
								?>
						</div>        
							

					</div><!--task-details-->


				</div>
				<div id="footer">
					&copy CMM Technologies Pvt Ltd
				</div>
		</div><!--wrapper-->

			<div id="popupAddTask" class="Closebtn" ><!--add task popup-->

					

		 </div><!--add task popup-->


		 <div id="popup_ex" class="Closebtn" ><!--request extension popup-->

					

		 </div><!--request extension popup-->

		 
		 <div class="popups" id="profilePopup"><!--profile popup-->
			<?php

			$page->showProfileTable($user);
			?>
		 </div><!--profile popup-->

		
		 <div class="popups" id="editProfilePopup"><!--editProfile popup-->
			<?php

			$page->editProfileTable($user);
			?>
		 </div><!--editProfile popup-->



		<div class="popups" id="changePasswordPopup"><!--password popup-->
			<?php

			$page->changePassword($user);
			?>
		 </div><!--password popup-->
		
		 <div class="popups" id="updateProgressPopup"><!--updateProgress popup-->
			<?php

			$page->updateProgress();
			?>
		 </div><!--updateProgress popup-->





		 <div id="background"></div>
	</body>
</html>
